package com.d16_Socket_Homework.homework3;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class ClientDemo3_TCP {
    public static void main(String[] args) throws IOException {
        //键盘录入
        Scanner sc = new Scanner(System.in);
        //创建发送端Socket对象，指定发送的ip，发送目的地
        Socket socket = new Socket("127.0.0.1", 6666);
        //开流写数据
        BufferedWriter bw = new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream())
        );
        BufferedReader br = new BufferedReader(
                new InputStreamReader(socket.getInputStream())
        );
        //不停的发送数据，和接收服务器反馈
        while (true){
            //键盘录入
            System.out.println("请输入一段文本：");
            String s = sc.nextLine();
            //判断标记，录入该标记时结束发送
            if(s.equals("886")){
                socket.shutdownOutput();
                break;
            }
            //写数据
            bw.write(s);
            bw.newLine();
            bw.flush();
            System.out.println("服务器返回：");
            //打印服务器反馈
            System.out.println(br.readLine());
        }
    }
}
